package org.smartercontext.contextmger.utilities;

import java.util.ArrayList;
import java.util.List;

import org.smartercontext.contextmger.DTO.ProductDTO;
import org.smartercontext.contextmger.contextsphere.ContextManagerController;

import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

public class GetProducts {

	private List<ProductDTO>wishlist;
	private List<ProductDTO>ranked;
	
	public void getListProducts(Model usermodel, String username){
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		OntModel modelProducts = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		modelProducts.setNsPrefixes(ContextManagerController.getMappings());
		Property wishesP = modelProducts.createProperty(ContextManagerController.getNsSchemaShopping(),"wishes");
		Property rankedP = modelProducts.createProperty(ContextManagerController.getNsSchemaShopping(),"ranked");
		DatatypeProperty rankingValue = modelProducts.createDatatypeProperty(ContextManagerController.
				getNsSchemaPERSONAL()+"rankingValue");
		
		Resource resource = usermodel.getResource(nsUser+username);
		wishlist = new ArrayList<>();
		ranked = new ArrayList<>();
		
		StmtIterator iteratorWish = resource.listProperties(wishesP);
		while(iteratorWish.hasNext()){
			Statement stmt = iteratorWish.next();
			String object = stmt.getObject().toString();
			ProductDTO pdto = new ProductDTO();
			pdto.setName(object.split("#")[1]);
			wishlist.add(pdto);
			
			
		}
		
		StmtIterator iteratorPR = resource.listProperties(rankedP);
		while(iteratorPR.hasNext()){
			Statement stmt = iteratorPR.next();
			String object = stmt.getObject().toString();
			
			Resource product = usermodel.getResource(object);
			Statement stmt2 = product.getProperty(rankingValue);
			String value = stmt2.getObject().asLiteral().getString();
			
			ProductDTO pdto = new ProductDTO();
			pdto.setName(object.split("#")[1]);
			pdto.setRating(value);
			ranked.add(pdto);
		}
		
		
	}
	
	public Model deleteProductToUserModel(Model usermodel, String username, String nameToDelete, String type){
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		String nsProducts="http://smartercontext.org/vocabularies/rdf/products.rdf#";
		Resource resource = usermodel.getResource(nsUser+username);
		OntModel modelProducts = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		modelProducts.setNsPrefixes(ContextManagerController.getMappings());
		Property wishesP = modelProducts.createProperty(ContextManagerController.getNsSchemaShopping(),"wishes");
		Property rankedP = modelProducts.createProperty(ContextManagerController.getNsSchemaShopping(),"ranked");
		
		if(type.equals("prank")){
			
			StmtIterator iterator = resource.listProperties(rankedP);
			Statement stmt = null;
			while(iterator.hasNext()){
				 Statement stsmtx = iterator.next();
				 String object = stsmtx.getObject().toString();
				 if(object.equals(nsProducts+nameToDelete)){
					 stmt = stsmtx;
				 }
			}
			
			if(stmt!=null){
				usermodel.remove(stmt);
			}
			
		}else if(type.equals("pwish")){
			StmtIterator iterator = resource.listProperties(wishesP);
			Statement stmt = null;
			while(iterator.hasNext()){
				 Statement stsmtx = iterator.next();
				 String object = stsmtx.getObject().toString();
				 if(object.equals(nsProducts+nameToDelete)){
					 stmt = stsmtx;
				 }
			}
			
			if(stmt!=null){
				usermodel.remove(stmt);
			}
		}
		
		return usermodel;
		
	}
	
	public Model updateProductRatingToUserModel(Model usermodel){
		
		String nsProducts="http://smartercontext.org/vocabularies/rdf/products.rdf#";
		OntModel modelProducts = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		modelProducts.setNsPrefixes(ContextManagerController.getMappings());
		DatatypeProperty rankingValue = modelProducts.createDatatypeProperty(ContextManagerController.
				getNsSchemaPERSONAL()+"rankingValue");
		
		for(int i=0;i<ranked.size();i++){
			ProductDTO pdto = ranked.get(i);
			String name = pdto.getName();
			String value = pdto.getRating();
			
			Resource product = usermodel.getResource(nsProducts+name);
			if(product!=null){
				product.removeAll(rankingValue);
				Literal valueL = modelProducts.createTypedLiteral((Integer.parseInt(value)));
				usermodel.addLiteral(product, rankingValue, valueL);
			}
					
			
		}
		
		return usermodel;
		
	}

	public List<ProductDTO> getWishlist() {
		return wishlist;
	}

	public void setWishlist(List<ProductDTO> wishlist) {
		this.wishlist = wishlist;
	}

	public List<ProductDTO> getRanked() {
		return ranked;
	}

	public void setRanked(List<ProductDTO> ranked) {
		this.ranked = ranked;
	}
	
	
	
}
